Define a default #to_s in Drop.

Akinori MUSHA 10 years ago
parent
commit
23bdb7be7b
2 changed files with 38 additions and 0 deletions
  1. 4 0
      app/concerns/liquid_droppable.rb
  2. 34 0
      spec/concerns/liquid_droppable_spec.rb

+ 4 - 0
app/concerns/liquid_droppable.rb

@@ -9,6 +9,10 @@ module LiquidDroppable
9 9
       @object = object
10 10
     end
11 11
 
12
+    def to_s
13
+      @object.to_s
14
+    end
15
+
12 16
     def each
13 17
       (public_instance_methods - Drop.public_instance_methods).each { |name|
14 18
         yield [name, __send__(name)]

+ 34 - 0
spec/concerns/liquid_droppable_spec.rb

@@ -0,0 +1,34 @@
1
+require 'spec_helper'
2
+
3
+describe LiquidDroppable do
4
+  before do
5
+    class DroppableTest
6
+      include LiquidDroppable
7
+
8
+      def initialize(value)
9
+        @value = value
10
+      end
11
+
12
+      attr_reader :value
13
+
14
+      def to_s
15
+        "[value:#{value}]"
16
+      end
17
+    end
18
+
19
+    class DroppableTestDrop
20
+      def value
21
+        @object.value
22
+      end
23
+    end
24
+  end
25
+
26
+  describe 'test class' do
27
+    it 'should be droppable' do
28
+      five = DroppableTest.new(5)
29
+      five.to_liquid.class.should == DroppableTestDrop
30
+      Liquid::Template.parse('{{ x.value | plus:3 }}').render('x' => five).should == '8'
31
+      Liquid::Template.parse('{{ x }}').render('x' => five).should == '[value:5]'
32
+    end
33
+  end
34
+end